package com.briup.mt.service.impl;

import com.briup.mt.bean.Article;
import com.briup.mt.bean.ArticleExample;
import com.briup.mt.dao.ArticleMapper;
import com.briup.mt.service.IArticleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service
public class  ArticleServiceImpl implements IArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Override

    public List<Article> query(String title, Long categoryId) {

        ArticleExample example = new ArticleExample();
        if(!StringUtils.isEmpty(title)){
            example.createCriteria().andTitleLike("%\"+title+\"%");
        }
        if(!StringUtils.isEmpty(categoryId)){
            example.createCriteria().andCategoryIdEqualTo(categoryId);
        }

        List<Article> list = articleMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveOrUpdate(Article article) {
        if(article.getId() == null){
            articleMapper.insert(article);
        } else {
            articleMapper.updateByPrimaryKey(article);
        }
    }

    @Override
    public void deleteById(long id) {
       articleMapper.deleteByPrimaryKey(id);
    }
}
